name: Version bump

on:
  workflow_dispatch:
    inputs:
      bump:
        description: "Version bump type"
        required: true
        type: choice
        default: auto
        options:
        - auto
        - patch
        - minor
        - major
      prerelease:
        description: "Increase to this prerelease version"
        required: false
        type: choice
        default: none
        options:
        - none
        - alpha
        - beta
        - rc

jobs:
  create_release_branch_pr:
    runs-on: ubuntu-latest
    env:
      GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
    permissions:
      contents: write       # to create a github release
      pull-requests: write  # to create and update PRs

    steps:
    - uses: actions/checkout@v4.1.4
      with:
        fetch-depth: 0

    - name: Set up Python
      uses: actions/setup-python@v5.1.0
      with:
        python-version: "3.10"
        architecture: x64

    - name: Bump version
      id: cz-bump
      uses: commitizen-tools/commitizen-action@0.21.0
      with:
        increment: ${{ github.event.inputs.bump != 'auto' && github.event.inputs.bump || '' }}
        prerelease: ${{ github.event.inputs.prerelease != 'none' && github.event.inputs.prerelease || '' }}
        commit: "false"
        push: "false"
        changelog: ${{ github.event.inputs.prerelease == 'none' && 'true' || 'false' }}
        github_token: ${{ secrets.MELTYBOT_GITHUB_AUTH_TOKEN }}
        extra_requirements: 'git+https://github.com/meltano/commitizen-version-bump@main'
        changelog_increment_filename: ${{ github.event.inputs.prerelease == 'none' && '_changelog_fragment.md' || '' }}

    - name: Draft Release
      id: draft-release
      uses: softprops/action-gh-release@v2
      with:
        draft: true
        body_path: ${{ github.event.inputs.prerelease == 'none' && '_changelog_fragment.md' || '' }}
        tag_name: v${{ steps.cz-bump.outputs.version }}
        prerelease: ${{ github.event.inputs.prerelease != 'none' }}
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    - name: Create Pull Request
      uses: peter-evans/create-pull-request@v6
      id: create-pull-request
      with:
        # https://github.com/peter-evans/create-pull-request
        token: ${{ secrets.MELTYBOT_GITHUB_AUTH_TOKEN }}
        commit-message: "chore: Bump package version"
        title: "chore: Release v${{ steps.cz-bump.outputs.version }}"
        body: |
          Bump changelog for release v${{ steps.cz-bump.outputs.version }}
        branch: release/v${{ steps.cz-bump.outputs.version }}
        base: main
        labels: release
